package com.shangma.cn.controller;

import com.shangma.cn.commons.http.AxiosResult;
import com.shangma.cn.commons.page.PageBean;
import com.shangma.cn.commons.valid.AddGroup;
import com.shangma.cn.commons.valid.UpdateGroup;
import com.shangma.cn.controller.base.BaseController;
import com.shangma.cn.dto.base.impl.BrandDTO;
import com.shangma.cn.dto.base.impl.RoleDTO;
import com.shangma.cn.entity.base.impl.Brand;
import com.shangma.cn.entity.base.impl.Role;
import com.shangma.cn.perm.HasPerm;
import com.shangma.cn.query.impl.BrandQuery;
import com.shangma.cn.query.impl.RoleQuery;
import com.shangma.cn.service.BrandService;
import com.shangma.cn.service.RoleService;
import com.shangma.cn.transfer.base.impl.BrandTransfer;
import com.shangma.cn.transfer.base.impl.RoleTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/7  16:42
 * 文件含义:
 */
@RestController
@RequestMapping("role")
@RequiredArgsConstructor
public class RoleController extends BaseController {

    /*
     * 前后端分离的时候
     *  前端和后台 可能不是一个人写的
     *
     * 当前端访问后台的接口时
     *       1： 怎么知道请求路径的
     *       2： 怎么知道需要什么参数
     *       3： 怎么知道返回值的
     *
     * 此时我们前后端程序员需要一个接口文档
     *    1: 在线的
     *         swagger2 knife4j
     *
     *
     *    2: 纸质版的 （电子版的）
     *
     *
     *    3： postman
     *
     *
     *
     *
     *
     *
     *
     * */

    public final RoleService roleService;

    public final RoleTransfer roleTransfer;

    @GetMapping("list")
    public AxiosResult<List<RoleDTO>> list() {
        return AxiosResult.success(roleTransfer.toDTOS(roleService.list()));
    }


    /**
     * 分页条件查询
     *
     * @return
     */
    @GetMapping
    @HasPerm(permSign = "sys:role:list")
    public AxiosResult<PageBean<RoleDTO>> searchPage(RoleQuery roleQuery) {
        return AxiosResult.success(roleService.searchPage(roleQuery));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AxiosResult<RoleDTO> getById(@PathVariable long id) {
        return AxiosResult.success(roleTransfer.toDTO(roleService.findOne(id)));
    }


    @PostMapping
    @HasPerm(permSign = "sys:role:add")
    public AxiosResult<Void> add(@RequestBody @Validated(AddGroup.class) Role role) {
        return toAjax(roleService.addEntity(role));
    }

    @PutMapping
    @HasPerm(permSign = "sys:role:update")
    public AxiosResult<Void> update(@RequestBody @Validated(UpdateGroup.class) Role role) {
        return toAjax(roleService.updateEntity(role));
    }

    @DeleteMapping("{id}")
    public AxiosResult<Void> deleteById(@PathVariable long id) {
        return toAjax(roleService.deleteById(id));
    }

    @DeleteMapping("batch/{ids}")
    @HasPerm(permSign = "sys:role:delete")
    public AxiosResult<Void> batchDelete(@PathVariable List<Long> ids) {
        return toAjax(roleService.deleteByIds(ids));
    }


    @PutMapping("{roleId}/menu/{menuIds}")
    @HasPerm(permSign = "sys:role:grant")
    public AxiosResult<Void> setRoleMenu(@PathVariable Long roleId, @PathVariable List<Long> menuIds) {
        int row = roleService.setRoleMenu(roleId, menuIds);
        return toAjax(row);
    }

    @GetMapping("{id}/menus")
    public AxiosResult<List<Long>> getRoleMenusById(@PathVariable Long id) {
        List<Long> menuIds = roleService.getRoleMenusById(id);
        return AxiosResult.success(menuIds);
    }
}
